package org.ofbiz.steel.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class SteelModelServices {
	public static final String module = SteelModelServices.class.getName();

	public static final String OFBIZ_HOME = System.getProperty("ofbiz.home");
	public static final String filePath  = "/applications/machine/entitydef/";
	
	
	// 保存表定义到硬盘的xml文件内
		public static  Map<String,Object> addEntityToModelXml(DispatchContext ctx, Map<String, ? extends Object> context){
			Debug.log("addEntityToModelXml   === start");
			Map<String,Object> result = ServiceUtil.returnSuccess();
			Delegator delegator = ctx.getDelegator();
			List<ModelEntity> nfsList=new ArrayList();
			
			
			 String fileName =OFBIZ_HOME+filePath+"form2_model.xml";
			 
			try {
				
				
				 ModelEntity modelEntity= new ModelEntity();
				 modelEntity.setEntityName("hahah4");
				 
				 modelEntity.setPackageName("org.ofbiz.machine");
				 
				 ModelField baseId =createField(modelEntity, "主键","baseId", "id", true);
				 ModelField name =createField(modelEntity,"名称", "name", "name", false);
				
				
				 modelEntity.addField(baseId);
				 modelEntity.addField(name);
				 
				 
				
				 nfsList.add(modelEntity);
				 
				 
				File file = new File(fileName);
	        	Document mainDoc = UtilXml.readXmlDocument(file.toURL());
				addToXmlDocument(nfsList,mainDoc);
	        	 
	        	 UtilXml.writeXmlDocument(fileName, mainDoc);
			} catch (SAXException | ParserConfigurationException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	 
	        Debug.log("addEntityToModelXml   === end");
			return result;
		}
		
		
		public static void main(String[] args) throws Exception {
			
			String fileName ="D:\\workspace\\cross_server\\applications\\machine\\entitydef\\auto_model1.xml";

			
			 try {
				 List<ModelEntity> nfsList=new ArrayList();
				 
				 ModelEntity modelEntity= new ModelEntity();
				 modelEntity.setEntityName("hahah4");
				 
//				 ModelField baseId =ModelField.create(modelEntity, "baseId", "id", true);
//				 ModelField name =ModelField.create(modelEntity, "name", "name", false);
				 
				 ModelField baseId =createField(modelEntity, "主键","baseId", "id", true);
				 ModelField name =createField(modelEntity,"名称", "name", "name", false);
				
				 modelEntity.addField(baseId);
				 modelEntity.addField(name);
	        	 
				 nfsList.add(modelEntity);
				 //新建空文件
	        	//Document mainDoc = makeXmlDocument(nfsList);
	        	 
				 //不停添加到xml文件中
				 File file = new File(fileName);
	        	 Document mainDoc = UtilXml.readXmlDocument(file.toURL());
	        	 addToXmlDocument(nfsList,mainDoc);
	        	 
	        	 UtilXml.writeXmlDocument(fileName, mainDoc);
			} catch (IOException e) {
				//Debug.logError(e.getMessage(), module);
				
			}


		}
		private static ModelField createField(ModelEntity modelEntity,String description,  String name, String type, boolean isPk) {
	        return ModelField.create(modelEntity, description, name, type, null, null, null, false, isPk, false, false, false, null);
	    }
		 // ======= XML Related Methods ========
		private static Document makeXmlDocument(Collection<ModelEntity> values) {
	        Document document = UtilXml.makeEmptyXmlDocument("entitymodel");

	        if (document == null) return null;

	        addToXmlDocument(values, document);
	        return document;
	    }
		private static int addToXmlDocument(Collection<ModelEntity> values, Document document) {
	        return addToXmlElement(values, document, document.getDocumentElement());
	    }
		
		private static int addToXmlElement(Collection<ModelEntity> values, Document document, Element element) {
	        if (values == null) return 0;
	        if (document == null) return 0;

	        int numberAdded = 0;

	        for (ModelEntity value: values) {
	            Element valueElement = value.toXmlElement(document);

	            element.appendChild(valueElement);
	            numberAdded++;
	        }
	        return numberAdded;
	    }
}
